package org.jackysoft.report.rows;

import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.jackysoft.entity.ArmItem;
import org.jackysoft.entity.Department;
import org.jackysoft.entity.Product;
import org.jackysoft.entity.ProductType;
import org.jackysoft.report.DataItemType;
import org.jackysoft.report.DataItemUtil;
import org.jackysoft.service.ProductService;
import org.jackysoft.util.AbstractRowWatcher;
import org.jackysoft.util.ExcelUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ArmItemSemiRowWatcher extends AbstractRowWatcher {
	static final Logger logger = LoggerFactory.getLogger(ArmItemSemiRowWatcher.class);
	private Collection<ArmItem> _arms;
	private ProductService productService;
	private Department dept;
	private DataItemUtil dataItemUtil;
	private boolean checkNew;
	private ProductType _pType;
	public ArmItemSemiRowWatcher(Collection<ArmItem> arms,
			ProductService productService, Department dept,
			DataItemUtil dataItemUtil, boolean b, ProductType ptype) {
		super();
		this._arms = arms;
		this.productService = productService;
		this.dept = dept;
		this.dataItemUtil = dataItemUtil;
		this.checkNew = b;
		this._pType = ptype;
	}

	@Override
	public void accept(Row row) {
		try {
			Iterator<Cell> cellItr = row.cellIterator();

			// 0 代码
			// 1 名称
			// 2 单位
			// 3 年初
			// 4 月初
			// 5 收入
			// 6 收入累计
			// 7 发出
			// 8 发出累计
			// 9 废品
			// 10 废品累计
			// 11盈亏
			// 12 盈亏累计
			// 13 待处理
			// 14 备注
			Cell code = cellItr.next();
			Cell name = cellItr.next();
			Cell punit = cellItr.next();
			String nameVal = StringUtils.trim(name.getStringCellValue());
			String punitVal = StringUtils.trim(punit.getStringCellValue());
			String codeVal = StringUtils.trim(code.getStringCellValue());

			Product pdt = productService.get(codeVal);
			if (checkNew && pdt == null) {// 产品不存在则加入库里
				Product tmpdt = new Product();
				tmpdt.setCode(codeVal);
				tmpdt.setName(nameVal);
				tmpdt.setPriceUnit(punitVal);
				tmpdt.getProviders().add(dept);
				tmpdt.setItemType(this._pType);
				productService.create(tmpdt);
				pdt = tmpdt;
			}

			Cell year  	= cellItr.next();// 3 年初
			Cell month 	= cellItr.next();// 4 月初
			Cell in    	= cellItr.next();// 5 月入
			Cell inAcl 	= cellItr.next();// 6 入累计
			Cell out   	= cellItr.next();// 7月出
			Cell outAcl = cellItr.next();// 8出累计
			Cell waster 	= cellItr.next();// 9废品
			Cell wasterAcl= cellItr.next();// 10废品累计
			Cell inc    = cellItr.next();// 11盈亏
			Cell incAcl = cellItr.next();// 12盈亏累计
			Cell will   = cellItr.next();// 13待处理
			Cell remark = cellItr.next();// 14备注
			ArmItem arm = new ArmItem();
			arm.setHost(pdt);
			arm.setDepartment(dept);
			arm.setRemark(remark.getStringCellValue());
			arm.setItemType(this._pType);

			dataItemUtil.setDataItemValue(arm, DataItemType.ForwardStore, ExcelUtil.cellNumber(year), ExcelUtil.cellNumber(month));
			dataItemUtil.setDataItemValue(arm, DataItemType.LocalIn,	  ExcelUtil.cellNumber(in), ExcelUtil.cellNumber(inAcl));
			dataItemUtil.setDataItemValue(arm, DataItemType.LocalOut,	  ExcelUtil.cellNumber(out), ExcelUtil.cellNumber(outAcl));
			dataItemUtil.setDataItemValue(arm, DataItemType.Waster,		  ExcelUtil.cellNumber(waster), ExcelUtil.cellNumber(wasterAcl));
			dataItemUtil.setDataItemValue(arm, DataItemType.Increment,	  ExcelUtil.cellNumber(inc), ExcelUtil.cellNumber(incAcl));
			dataItemUtil.setDataItemValue(arm, DataItemType.Willed,	      ExcelUtil.cellNumber(will), 0D);
			
			_arms.add(arm);
		} catch (NumberFormatException e) {
			logger.error("error for get line with ",e);
		} catch (IllegalStateException e){
			logger.error("error for get line with ",e);
		}


	}
	

}
